//
//  HELConsultVC.m
//  HandheldELearn
//
//  Created by wsj2015 on 2017/1/8.
//  Copyright © 2017年 陈武琦. All rights reserved.
//

#import "HELConsultVC.h"
#import "BTConsultCell.h"
#import "BTSearchBar.h"
//#import "SGScanningQRCodeVC.h"
#import "MessageServicer.h"
#import "HELSearchHistoryVC.h"
#import "UINavigationController+WXSTransition.h"
#import "UIView+WZLBadge.h"
#import "BTBaseWebVC.h"
#import "HELMessageListVC.h"
@interface HELConsultVC ()<UISearchBarDelegate>
@property (nonatomic,  strong) BTSearchBar *searchBar;
@property (nonatomic,  strong) UIButton *messageBtn;

@end

@implementation HELConsultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (BTSearchBar *)searchBar{
    if (!_searchBar) {
        _searchBar = [BTSearchBar searchBarWithDelegate:self];
        
    }
    return _searchBar;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getNotMessage];
}

- (void)configUI{
    
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64 -49);
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight= UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 300;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"BTConsultCell" bundle:nil] forCellReuseIdentifier:@"BTConsultCell"];
    self.navigationItem.titleView = self.searchBar;
    
    //消息按钮
    UIButton *btn =[UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(0, 0, 33, 33);
    [btn setImage:[UIImage imageNamed:@"消息"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(messageNot) forControlEvents:UIControlEventTouchUpInside];
    self.messageBtn = btn;
    btn.badgeBgColor = [UIColor whiteColor];
    [btn showBadgeWithStyle:WBadgeStyleRedDot value:1 animationType:WBadgeAnimTypeBreathe];
    btn.badgeCenterOffset = CGPointMake(-8, 10);
    UIBarButtonItem *right1 =[[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.navigationItem.titleView = self.searchBar;
    UIBarButtonItem *fixItem =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixItem.width = -100;
    
    UIBarButtonItem *menuBtn =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"扫一扫 copy"] style:UIBarButtonItemStyleDone target:self action:@selector(menuAction)];
    
    self.navigationItem.rightBarButtonItems =@[right1,fixItem,menuBtn];
    self.title  =@"资讯";
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf getConsultList:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
        [weakSelf getConsultList:NO];
    }];
    [self getConsultList:YES];
    
}
#pragma mark 点击进入消息通知
- (void)messageNot{
    
    HELMessageListVC *vc =[HELMessageListVC new];
    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark -二维码扫描
-(void)menuAction{
    
    //    [self.navigationController wxs_pushViewController:[HELSignInVC new] animationType:WXSTransitionAnimationTypeBrickOpenVertical];
    
    
//    SGScanningQRCodeVC *scan =[[SGScanningQRCodeVC alloc] init];
//    
//    weakSelf_define;
//    scan.callback  =^(NSString *qrcode,NSString *tureName){
//        
//        HELMySignInVC *vc = [HELMySignInVC new];
//        vc.courseId = qrcode;
//      
//        [weakSelf.navigationController pushViewController:vc animated:YES];
//        
//        
//    };
//    HELNavigationContrller *nav = [[HELNavigationContrller alloc] initWithRootViewController:scan];
//    
//    [self wxs_presentViewController:nav animationType:WXSTransitionAnimationTypeSysCubeFromLeft completion:nil];
//  
//    
    
    
    
    
}
#pragma -mark 请求通知未读数
- (void)getNotMessage{
    weakSelf_define;
    [[MessageServicer shareServicer] requestPersonInfoWithSucess:^(NSInteger unread) {
        
        unread >0?[weakSelf.messageBtn resumeBadge]:[weakSelf.messageBtn clearBadge];
        
    }];
}


- (void)getConsultList:(BOOL)isRecover{
    
    static NSInteger  page =0;
    
    if (isRecover) {
        page = 0;
    }else{
        page ++;
        
    }
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] POST:kConsultList parameters:nil success:^(id responseObject) {
        weakSelf.tableView.loading = NO;
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
        
        if ([responseObject[@"data"][@"data"] isKindOfClass:[NSArray class]]) {
            NSInteger totalPage;
            
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]] ) {
                totalPage = 0;
            }else{
                totalPage = [responseObject[@"data"][@"totalPage"] integerValue];;
            }
            //从0 开始
            NSInteger currentPage = [responseObject[@"number"] integerValue];
            if (currentPage+1 >=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
                
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
                
            }
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
                
            }else{
                
            }
            [weakSelf.dataArray addObjectsFromArray:[BTCosultModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            
            [weakSelf.tableView reloadData];
        }
        
        
    } failure:^(NSString *err) {
        weakSelf.tableView.loading = NO;
        [weakSelf.tableView.footer endRefreshing];
        [weakSelf.tableView.header endRefreshing];
    }];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
   return  self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BTConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BTConsultCell" forIndexPath:indexPath];
    BTCosultModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    BTCosultModel *model = self.dataArray[indexPath.row];
    BTBaseWebVC *webVC = [[BTBaseWebVC alloc] init];
    NSString *url ;
    url = [NSString stringWithFormat:@"%@/%@/%@",[HELNetworkManager sharedMg].bathURL,kCousultDetail,model.id];
    webVC.theUrl = [NSURL URLWithString:url];
    webVC.title = model.subjectName;
    [self.navigationController pushViewController:webVC animated:YES];
    
    
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    HELSearchHistoryVC *vc = [HELSearchHistoryVC new];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    return NO;
    return NO;
}



@end
